using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using jqMvcGrid.Models;
using System.Web.UI.MobileControls;
using System.Web.UI;
using System.Collections;

namespace jqMvcGrid.Controllers
{
    public class TwoGridsController : Controller
    {
        private IProductRepository _repository = new ProductRepository();

        private List<Product> tmpGrid2 = new List<Product>();

        private static int rowsOfGrid1 = 1;
        private static int rowsOfGrid2 = 10;

        public TwoGridsController()
        {
            ViewData["chkChooser"] = "true";


        }

        public ActionResult Lookup(string q, int limit)
        {
            var list = _repository.Index();
            var data = from s in list select new { s.ProductName, s.QuantityPerUnit };
            return Json(data);
        }

        //
        // GET: /TwoGrids/

        public ActionResult Index()
        {
            return View();
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Save(FormCollection form)
        {
            // Save all
            if ((List<Product>)Session["tmpGrid2"] != null)
                tmpGrid2 = (List<Product>)Session["tmpGrid2"];
            return RedirectToAction("Index");
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Index(FormCollection form)
        {
            int tmp = rowsOfGrid1;
            rowsOfGrid1 = rowsOfGrid2;
            rowsOfGrid2 = tmp;
            ViewData["chkChooser"] = (rowsOfGrid1 < rowsOfGrid2).ToString();
            return RedirectToAction("Index");
        }

        public ActionResult List1(int page, int rows, string search, string sidx, string sord, int? categoryId)
        {
            var repository = new ProductRepository();

            var products = new List<Product>();
            products.AddRange(NewProductRows(rowsOfGrid1));
            var model = from entity in products.AsQueryable().OrderBy(sidx + " " + sord)
                        select new
                        {
                            ProductID = entity.ProductID,
                            ProductName = entity.ProductName,
                            UnitPrice = entity.UnitPrice
                        };
            return Json(model.ToJqGridData(page, rows, null, search,
                new[] { "ProductName" }));
        }

        public ActionResult List2(int page, int rows, string search, string sidx, string sord, int? categoryId)
        {
            try
            {
                if ((List<Product>)Session["tmpGrid2"] != null)
                    tmpGrid2 = (List<Product>)Session["tmpGrid2"];
            }
            catch { }

            var repository = new ProductRepository();

            var products = new List<Product>();
            products.AddRange(NewProductRows(rowsOfGrid2));
            var model = from entity in products.AsQueryable().OrderBy(sidx + " " + sord)
                        select new
                        {
                            ProductID = entity.ProductID,
                            ProductName = entity.ProductName,
                            UnitPrice = entity.UnitPrice
                        };
            return Json(model.ToJqGridData(page, rows, null, search,
                new[] { "ProductName" }));
        }

        private IList<Product> NewProductRows(int rows)
        {
            try
            {
                var products = new List<Product>();
                for (int i = 0; i < rows; i++)
                {
                    products.Insert(i, new Product() { ProductID = i, ProductName = "", SupplierID = 0, CategoryID = 0, QuantityPerUnit = "", UnitPrice = 0.00m, UnitsInStock = 0, UnitsOnOrder = 0, ReorderLevel = 0, Discontinued = false });
                }
                return products;
            }
            catch { return null; }
        }

        //
        // GET: /TwoGrids/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /TwoGrids/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /TwoGrids/Create

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Create1(Product product, FormCollection collection)
        {
            try
            {
                // if ok

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Create2(Product product, FormCollection collection)
        {
            try
            {
                try
                {
                    if ((List<Product>)Session["tmpGrid2"] != null)
                        tmpGrid2 = (List<Product>)Session["tmpGrid2"];
                }
                catch { }

                try
                {
                    tmpGrid2.RemoveAt(product.ProductID);
                }
                catch { }

                // validate
                if (product.ProductName.Length > 0)
                {
                    try
                    {
                        for (int i = tmpGrid2.Count(); i < product.ProductID; i++)
                        {
                            tmpGrid2.Insert(i, new Product() { });
                        }
                    }
                    catch { }
                    tmpGrid2.Insert(product.ProductID, product);
                }

                Session["tmpGrid2"] = tmpGrid2;

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /TwoGrids/Edit/5

        public ActionResult Edit(int id)
        {
            return View();
        }

        //
        // POST: /TwoGrids/Edit/5

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
